package com.FH.applyFrom.apply_card;

import com.FH.organization.Attachment.AttachmentResource;
import com.FH.organization.department.DepartmentResource;
import com.FH.organization.operator_log.OperatorLogPullDTO;
import com.FH.organization.operator_log.OperatorLogResource;
import com.FH.organization.people.KamMemberResource;
import com.FH.organization.suggestion.SuggestionResource;
import com.FH.organization.work_flow.WorkFlowPullDTO;
import com.FH.organization.work_flow.WorkFlowResource;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/12/16.
 */
@Stateless
public class ApplyCardImpl extends AccessDB implements ApplyCardDAO {
    @Inject
    KamMemberResource kamMemberResource;
    @Inject
    OperatorLogResource operatorLogResource;
    @Inject
    DepartmentResource departmentResource;
    @Inject
    WorkFlowResource workFlowResource;
    @Inject
    AttachmentResource attachmentResource;
    @Inject
    SuggestionResource suggestionResource;


    @Override
    public ResultDTO getApplyCardInfo(String serach, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(serach);
            String existName = "0";

            String sqlexit = "select count(1) from apply_card  where 1=1 ";

            String sql_w = "";//查询条件sql组合
            String sql_page = "";//分页组合

            Integer page_ok = Integer.parseInt(page);//将定义page、size的String类型转换成Integer类型
            Integer size_ok = Integer.parseInt(size);

            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok + 1) + "," + size + "";
            }

            if (json.has("member_id")) {
                sql_w += " AND member_id = '" + json.getString("member_id") + "'";
            }
            if (json.has("apply_card_id")) {
                sql_w += " AND apply_card_id = '" + json.getString("apply_card_id") + "'";
            }
            if (json.has("isverify")) {
                sql_w += " AND isverify = '" + json.getString("isverify") + "'";
            }
//            else {
//                sql_w += " AND (isverify IS NULL OR isverify=0)";
//            }

            String sql_ok = sqlexit + sql_w + sql_page;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<ApplyCardShowDTO> result = new ArrayList<ApplyCardShowDTO>();

            if (!existName.equals("0")) {
                String sqlq = "select apply_card_id,apply_org,apply_men,apply_reason,member_id,fail_resource,create_time FROM apply_card where 1=1  " + sql_w + " ORDER BY create_time DESC" + sql_page;

                seresult = this.getReportingTo(sqlq);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {
                    ApplyCardShowDTO applyCardShowDTO = new ApplyCardShowDTO();
                    applyCardShowDTO.setApply_card_id(seresult.get(i)[1]);
                    applyCardShowDTO.setApply_org(seresult.get(i)[2]);
                    applyCardShowDTO.setApply_men(seresult.get(i)[3]);
                    applyCardShowDTO.setApply_reason(seresult.get(i)[4]);
                    applyCardShowDTO.setMember_id(seresult.get(i)[5]);
                    applyCardShowDTO.setFail_resource(seresult.get(i)[6]);
                    applyCardShowDTO.setCreate_time(seresult.get(i)[7]);
                    ResultDTO worklist = null, applymen = null, bespeakmen = null, suggestion = null;

                    if (json.has("isverify")) {
                        String state = json.getString("isverify");
                        worklist = workFlowResource.getWorkFlowInfo("{work_flow_formid:" + seresult.get(i)[1] + ",work_flow_state:" + state + "}", "1", "20");
                    } else {
                        worklist = workFlowResource.getWorkFlowInfo("{work_flow_formid:" + seresult.get(i)[1] + "}", "1", "20");
                    }

                    applyCardShowDTO.setWorkflow(worklist.getValues());
                    if (null != seresult.get(i)[3] && !"".equals(seresult.get(i)[3])) {
                        applymen = kamMemberResource.getUserListInfo("{member_id:" + seresult.get(i)[3] + "}", "1", "1");
                        applyCardShowDTO.setApply_menlist(applymen.getValues());
                    }

                    if (seresult.get(i)[1] != null && !seresult.get(i)[1].equals("")) {

                        suggestion = suggestionResource.getSuggestionInfo("{form_id:" + seresult.get(i)[1] + "}", "1", "100");
                        applyCardShowDTO.setSuggestionShowDTOs(suggestion.getValues());
                    }
                    result.add(applyCardShowDTO);
                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStr(result.size() + "");
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();

        }
        return resultDTO;
    }

    @Override
    public ResultDTO addApplyCardInfo(ApplyCardPullDTO applyCardPullDTO) {
        ResultDTO resultDTO = new ResultDTO();//构建返回对象


        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String existName = "";
        String apply_card_id = applyCardPullDTO.getApply_card_id();
        if (apply_card_id != null && !apply_card_id.equals("")) {

            String sql = "select count(1) from apply_card where apply_card_id='" + apply_card_id + "'";

            existName = getReportingToOne(sql);

            if (!existName.equals("0")) {
                return updateApplyCardInfo(applyCardPullDTO);
            }
        }

        String QUERY = "";

        //表中查看时间设为空值
        QUERY += "insert into apply_card(apply_card_id,apply_org,apply_men,apply_reason,member_id,create_time,isverify) values" +
                " ('" + uuid + "','" + applyCardPullDTO.getApply_org() + "','" + applyCardPullDTO.getApply_men()
                + "','" + applyCardPullDTO.getApply_reason() + "','" + applyCardPullDTO.getMember_id() + "','" + time + "','0')";
        int result = this.addReportingTo(QUERY);
        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStr(uuid);
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
            //日志操作
            OperatorLogPullDTO operatorlogPullDTO = new OperatorLogPullDTO();
            operatorlogPullDTO.setWork_form_id(uuid);
            operatorlogPullDTO.setOperator_log_type("补卡申请");
            operatorlogPullDTO.setOperator_log_title("补卡申请");
            operatorlogPullDTO.setOperator_log_content(applyCardPullDTO.getApply_reason());
            operatorlogPullDTO.setMember_id(applyCardPullDTO.getMember_id());
            operatorLogResource.addOperatorLogInfo(operatorlogPullDTO);

            //流程上报

            WorkFlowPullDTO workFlowPullDTO = new WorkFlowPullDTO();
            workFlowPullDTO.setWork_flow_formid(uuid);
            workFlowPullDTO.setWork_flow_type("apply_card");
            workFlowPullDTO.setWork_flow_desc("补卡请求审批");
            workFlowPullDTO.setWork_flow_date(time);
            workFlowPullDTO.setWork_flow_order("0");
            workFlowPullDTO.setWork_flow_createid(applyCardPullDTO.getMember_id());
            workFlowPullDTO.setWork_flow_approval(applyCardPullDTO.getReport_people());
            workFlowPullDTO.setWork_flow_approval_organization_id(applyCardPullDTO.getWork_flow_approval_organization_id());
            workFlowPullDTO.setFlow_path_id("");
            workFlowPullDTO.setWork_flow_state("0");
            workFlowPullDTO.setWork_flow_suggestion("");
            workFlowResource.addWorkFlowInfo(workFlowPullDTO);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }


    @Override
    public ResultDTO updateApplyCardInfo(ApplyCardPullDTO applyCardPullDTO) {
        ResultDTO resultDTO = new ResultDTO();

        String QUERY = "";

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());

        //更新sql语句
        QUERY += "update apply_card set create_time ='" + time + "'";

        if (null != applyCardPullDTO.getApply_org() && !"".equals(applyCardPullDTO.getApply_org())) {
            QUERY += ",apply_org ='" + applyCardPullDTO.getApply_org() + "'";
        }
        if (null != applyCardPullDTO.getApply_men() && !"".equals(applyCardPullDTO.getApply_men())) {
            QUERY += ",apply_men ='" + applyCardPullDTO.getApply_men() + "'";
        }
        if (null != applyCardPullDTO.getApply_reason() && !"".equals(applyCardPullDTO.getApply_reason())) {
            QUERY += ",apply_reason ='" + applyCardPullDTO.getApply_reason() + "'";
        }

        QUERY += " where apply_card_id = '" + applyCardPullDTO.getApply_card_id() + "'";
        int result = this.addReportingTo(QUERY);
        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStr(applyCardPullDTO.getApply_card_id());
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");
        }
        return resultDTO;
    }

    @Override
    public ResultDTO deleteApplyCardInfo(ApplyCardPullDTO applyCardPullDTO) {
        //删除之前查询是否有记录
        String sql = "select count(1) from apply_card where apply_card_id ='" + applyCardPullDTO.getApply_card_id() + "'";

        String existName = getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();

        if (!existName.equals("0")) {

            String QUERY = "";
            QUERY += "delete from apply_card where apply_card_id='" + applyCardPullDTO.getApply_card_id() + "'";

            result = this.addReportingTo(QUERY);
        }

        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }
}
